package com.stox.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.stox.manager.ExchangeManager;
import com.stox.model.Exchange;
import com.stox.repository.ExchangeRepository;

@Component
@Transactional
public class ExchangeManagerImpl implements ExchangeManager{
	
	@Autowired
	private ExchangeRepository exchangeRepository;
	
	@Override
	public long count() {
		return exchangeRepository.count();
	}
	
	@Override
	@Cacheable(value=cacheName, key="'country_' + #countryId")
	public List<Exchange> findByCountryId(Long countryId) {
		return exchangeRepository.findByCountryId(countryId);
	}

	@Override
	@Transactional
	@CachePut(value=cacheName, key="#exchange.id")
	public Exchange saveAndFlush(Exchange exchange) {
		return exchangeRepository.saveAndFlush(exchange);
	}
	
	@Override
	@Cacheable(cacheName)
	public Exchange findOne(Long id) {
		return exchangeRepository.findOne(id);
	}

}
